package fr.imag.m2pgi.m2m.geoloc.scip;

import static fr.imag.m2pgi.m2m.geoloc.scip.SCIP20.*;

import java.awt.Menu;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Scanner;

public class SCIP20Test {
  
  static private void enable(SCIP20 laser) throws IOException {
    System.out.println("Enabling laser...");
    laser.enableLaser();    
    System.out.println("Laser enabled.");
  }
  
  static private void disable(SCIP20 laser) throws IOException {
    System.out.println("Disable laser...");
    laser.disableLaser();
    System.out.println("Laser disabled.");
  }
  
  static private void version(SCIP20 laser) throws IOException {
    Map<String, String> m = laser.getVersion();
    System.out.println(m.get(KEY_VENDOR));
    System.out.println(m.get(KEY_PRODUCT));
    System.out.println(m.get(KEY_FIRMWARE));
    System.out.println(m.get(KEY_PROTOCOL));
    System.out.println(m.get(KEY_SERIAL));
  }
  
  static private void measure(SCIP20 laser) throws IOException {
    System.out.println("Getting 12bits measures...");
    ArrayList<Integer> points = laser.getTwoCharLastMeasure();
    System.out.println("Measured "+points.size()+" points.");
    System.out.println("Values:");
    for(int i=0; i<points.size(); i++) {
      System.out.println("  ["+i+"] : "+points.get(i));
    }
    
  }
  
  static private void menu() {
    System.out.println("*************************");
    System.out.println("*         Menu          *");
    System.out.println("*************************");
    System.out.println("* 1. Version            *");
    System.out.println("* 2. Enable             *");
    System.out.println("* 3. Measure            *");
    System.out.println("* 4. Measure angle...   *");
    System.out.println("* 5. Disable            *");
    System.out.println("* 6. Specifications     *");
    System.out.println("* 0. Quit               *");
    System.out.println("*************************");
  }
   
  public static void main(String[] args) {
    // listPorts();

    // SCIP20 laser = new SCIP20("/dev/ttyACM0");
    try {
      SCIP20 laser = new SCIP20(1701);
      
      int choice =0;
      Scanner s = new Scanner(System.in);
      menu();
      while((choice=Integer.parseInt(s.nextLine()))!=0) {
        switch(choice) {
        case 1: version(laser); break;
        case 2: enable(laser); break;
        case 3: measure(laser); break;
        case 4: measureAtAngle(laser); break;
        case 5: disable(laser); break;
        case 6: specs(laser); break;
        default:
          System.out.println("Unknown command");
        }
        menu();
      }
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  private static void measureAtAngle(SCIP20 laser) {
    Scanner s = new Scanner(System.in);
    System.out.println("Angle? ");
    int angle = s.nextInt();
    try {
      int dist = laser.getDistance(angle);
      System.out.println("Distance at angle "+angle+"° : "+dist+" mm.");
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  
  private static void specs(SCIP20 laser) throws IOException {
    Map<String, String> specif = laser.getSpecification();
    System.out.println(specif.get(SENSOR_MODEL));
    System.out.println(specif.get(MIN_MEASUREMENT));
    System.out.println(specif.get(MAX_MEASUREMENT));
    System.out.println(specif.get(TOTAL_STEPS));
    System.out.println(specif.get(FIRST_STEP_MEASURE));
    System.out.println(specif.get(LAST_STEP_MEASURE));
    System.out.println(specif.get(FRONT_STEP));
    System.out.println(specif.get(MOTOR_SPEED));
  }
}
